package boardgamer.common;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import java.util.Collections;
import java.util.List;

/**
 * Keeps track of events happening in the game
 *
 * @author garysoed
 */
public class GameLog<GS extends GameState, C extends GameStateChange> {

  private final List<GameUpdate<GS, C>> updates;

  public GameLog(GS initialGameState) {
    this.updates = Collections.synchronizedList(Lists.<GameUpdate<GS, C>>newLinkedList());
    this.add(new GameUpdate<GS, C>((C) null, initialGameState));
  }
  
  public GameLog(GameLog<GS, C> original) {
    synchronized (original.updates) {
      this.updates = Collections.synchronizedList(Lists.newLinkedList(original.updates));
    }
  }

  public void add(GameUpdate<GS, C> update) {
    synchronized (updates) {
      updates.add(update);
    }
  }

  public ImmutableList<GameUpdate<GS, C>> getUpdates(int firstIndex) {
    ImmutableList<GameUpdate<GS, C>> toReturn;
    synchronized (updates) {
      toReturn = ImmutableList.copyOf(updates.subList(firstIndex, updates.size()));
    }
    return toReturn;
  }
}
